package com.xmcc.filter;

import com.xmcc.pojo.SysUser;
import com.xmcc.utill.ThreadLocalCommon;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class LoginFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        SysUser sysUser = (SysUser) request.getSession().getAttribute("sysUser");
        //没有登录回到登录页面
        if (sysUser==null){
            response.sendRedirect("/signin.jsp");
            return;
        }
        //放入用户到线程类：
        ThreadLocalCommon.pushSysUser(sysUser);
        ThreadLocalCommon.pushHttpServletRequest(request);
        filterChain.doFilter(request,response);
    }

    @Override
    public void destroy() {

    }
}
